package fileexplorer.commands;

import java.io.File;
import java.util.ArrayList;


import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

import fileexplorer.FileObject;
import fileexplorer.View;

public class Supprimer extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		return null;
	}
	
	public static void del(final View view) {
		
		final ArrayList<FileObject> fileObjects = view.getFileObjectSelected();
		
		final Shell dialog = new Shell(view.shell);
    	dialog.setLayout(new FillLayout());
    	dialog.setText(Messages.Supprimer_Delete);
    	
    	Composite compV = new Composite(dialog, SWT.FILL);
    	FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
    	fillLayout.marginHeight = 10;
    	fillLayout.marginWidth = 10;
    	fillLayout.spacing = 5;
    	compV.setLayout(fillLayout);
    	
    	Composite compH1 = new Composite(compV, SWT.FILL);
    	compH1.setLayout(new RowLayout(SWT.HORIZONTAL));
    	
    	CLabel lbl = new CLabel(compH1, SWT.NULL);
    	String lstFile = ""; //$NON-NLS-1$
    	for(int i=0; i<fileObjects.size(); i++){
			FileObject fileObject = fileObjects.get(i);
			lstFile += fileObject.getNom();
			lstFile += " "; //$NON-NLS-1$
    	}
    	if(fileObjects.size()==1){
    		lbl.setText(Messages.Supprimer_AreYouSureYouWantToDeleteThisFile + lstFile);
    	}else{
    		lbl.setText(Messages.Supprimer_AreYouSureYouWantToDeleteTheseFiles + lstFile);
    	}
    	
    	Composite compH2 = new Composite(compV, SWT.FILL);
    	compH2.setLayout(new FillLayout(SWT.HORIZONTAL));
    	
    	Button btnCancel = new Button(compH2, SWT.NONE);
    	btnCancel.setText(Messages.Supprimer_No);
    	btnCancel.addMouseListener(new MouseListener() {
			@Override public void mouseUp(MouseEvent e) {}
			@Override
			public void mouseDown(MouseEvent e) {
				dialog.close();
			}
			@Override public void mouseDoubleClick(MouseEvent e) {}
		});
    	
    	Button btnRename = new Button(compH2, SWT.NONE);
    	btnRename.setText(Messages.Supprimer_Yes);
    	btnRename.addMouseListener(new MouseListener() {
			@Override public void mouseUp(MouseEvent e) {}
			@Override
			public void mouseDown(MouseEvent e) {
				
				for(int i=0; i<fileObjects.size(); i++){
					FileObject fileObject = fileObjects.get(i);
					File file = new File(fileObject.getPath());
					file.delete();
				}
				view.refreshIconView(view.getUrl());
				dialog.close();
				
			}
			@Override public void mouseDoubleClick(MouseEvent e) {}
		});
    	
    	dialog.pack();
    	dialog.open();

	}
}
